import Mock from 'mockjs'
import {param2Obj}from '../utils/myUtils'
let list=[];
let length=200
for(let i =0;i<length;i++){
  list.push({
    id:i+1,
    card:Mock.mock('@integer(100000000, 199999999)'),
    type:"普通会员",
    grade:"VIP"+Mock.mock('@integer(1, 3)'),
    name:Mock.mock('@cname'),
    phone:Mock.mock('@integer(10000000000, 19999999999)'),
    consumption:Mock.mock('@integer(1000, 20000)'),
    rest:Mock.mock('@integer(1000, 20000)'),
    store:Mock.mock('@integer(1000, 20000)'),
    score:Mock.mock('@integer(1000, 20000)'),
    startTime:Mock.mock('@date'),
    ddl:Mock.mock('@date'),
    state:true,
    operation:i+1,
    isShow:false
  })
}

export default{
  getVipData:(config)=>{
    console.log('config',param2Obj(config.url));
    let {search,page,pageSize} =param2Obj(config.url);
    
    let newlist=list.filter(item=>{
      if((search+"").length==9){
        return (item.card+"").indexOf(search)!=-1;
      }else if((search+"").length==11){
        return (item.phone+"").indexOf(search)!=-1;
      }else{
        return item.name.indexOf(search)!=-1;
      }
    })
    // 分页逻辑  (page-1)*pageSize<=index<page*pageSize
    let newlist2=list.filter((item,index)=>{
      return (page-1)*pageSize<=index&&index<page*pageSize;
    })
    return{
      code:200,
      msg:"请求成功",
      data1:newlist,
      data:newlist2,
      total:list.length,
    }
  },
  searchDate:(config)=>{
    let {sdate1,sdate2}=JSON.parse(config.body)
    let newlist3=list.filter(item=>{
      console.log(sdate1,sdate2);
      return ((item.startTime.split("-").join("")>=sdate1)&&(item.startTime.split("-").join("")<=sdate2));
    })
    console.log(newlist3)
    return{
      code:200,
      msg:"请求成功",
      data3:newlist3,
      // data:newlist2,
      total:list.length,
    }
  },
  delVipData:config=>{
    let id=JSON.parse(config.body).id;
    let newlist=list.filter((item)=>{
      return item.id!=id
    })
    list = newlist;
    return{
      code:200,
      msg:"删除成功",
      data:null,
    }
  },
  delbatchVipData:config=>{
    let ids=JSON.parse(config.body).ids;
    let newlist=list.filter((item)=>{
      return ids.indexOf(item.id)==-1
    })
    list = newlist;
    return{
      code:200,
      msg:"批量删除成功",
      data:null,
    }
  },
  addVIP:config=>{
    let {name,id,card,phone,type,date1}=JSON.parse(config.body);
    date1=date1.substr(0,10);
    list.push({
      id:id,
      card:card,
      type:type,
      grade:"VIP1",
      name:name,
      phone:phone,
      consumption:0,
      rest:0,
      store:0,
      score:0,
      startTime:date1,
      ddl:date1,
      state:true,
      operation:id,
      isShow:false
    })
    return{
      code:200,
      msg:"添加成功",
      data:null,
    }
  },
  editVIP:config=>{
    let {name,id,card,phone,type,date1}=JSON.parse(config.body);
    date1=date1.substr(0,10);
    list.forEach(item=>{
      if(item.id==id){
        item.id=id;
        item.name=name;
        item.card=card;
        item.phone=phone;
        item.type=type;
        item.date1=date1;
      }
    })
    return{
      code:200,
      msg:"修改成功",
      data:null,
    }
  }
}